package com.youmingxing.utils;

import android.app.ActivityManager;
import android.content.Context;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tools {

    /***是否为手机号**/
    public static boolean isMobileNum(String mobiles) {
        Pattern p = Pattern.compile("^1[3|4|5|7|8][0-9]\\d{4,8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 是否是合法邮箱
     */
    public static boolean isEmail(String email) {
        return email.contains("@") && email.contains(".") && email.split("@").length == 2
                && email.lastIndexOf("@") != email.length() - 1;
    }

    /***dp 转 px **/
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static String toK(int number) {
        if (number < 1000) {
            return number + "";
        }else if (number < 9999) {
            return number / 1000 + "." + (number % 1000) / 100 + "k";
        }else if (number < 99999) {
            return number / 1000 + "k";
        } else {
            return "99k+";
        }
    }


    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    return true;
                }else{
                    return false;
                }
            }
        }
        return false;
    }

}
